package com.smag.echarts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smag.common.core.domain.AjaxResult;
import com.smag.echarts.domain.EchartsLineBarSales;
import com.smag.echarts.domain.vo.EchartsLineBarSalesVo;
import com.smag.echarts.service.EchartsLineBarSalesService;
import com.smag.echarts.mapper.EchartsLineBarSalesMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author hzqhzq
 * @description 针对表【echarts_line_bar_sales】的数据库操作Service实现
 * @createDate 2023-04-21 16:44:29
 */
@Service
public class EchartsLineBarSalesServiceImpl extends ServiceImpl<EchartsLineBarSalesMapper, EchartsLineBarSales>
        implements EchartsLineBarSalesService {

    @Override
    public AjaxResult getSalesList() {
        List<EchartsLineBarSales> list = list();
        EchartsLineBarSales[] array = list.toArray(new EchartsLineBarSales[0]);
        Arrays.sort(array, Comparator.comparingInt(EchartsLineBarSales::getYear));
        List<Integer> year = new ArrayList<>();
        List<BigDecimal> number = new ArrayList<>();
        List<BigDecimal> value = new ArrayList<>();
        Arrays.stream(array).forEach(echartsLineBarSales -> {
            year.add(echartsLineBarSales.getYear());
            number.add(echartsLineBarSales.getNumber());
            value.add(echartsLineBarSales.getValue());
        });
        EchartsLineBarSalesVo echartsLineBarSalesVo = new EchartsLineBarSalesVo();
        echartsLineBarSalesVo.setYear(year);
        echartsLineBarSalesVo.setValue(value);
        echartsLineBarSalesVo.setNumber(number);
        return AjaxResult.success(echartsLineBarSalesVo);
    }

    @Override
    public List<EchartsLineBarSales> selectEchartsLineBarSalesList(EchartsLineBarSales echartsLineBarSales) {
        LambdaQueryWrapper<EchartsLineBarSales> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ObjectUtils.isNotNull(echartsLineBarSales.getYear()),EchartsLineBarSales::getYear,echartsLineBarSales.getYear());
        queryWrapper.like(ObjectUtils.isNotNull(echartsLineBarSales.getNumber()),EchartsLineBarSales::getNumber,echartsLineBarSales.getNumber());
        queryWrapper.like(ObjectUtils.isNotNull(echartsLineBarSales.getValue()),EchartsLineBarSales::getValue,echartsLineBarSales.getValue());
        return list(queryWrapper);
    }

    @Override
    public AjaxResult selectEchartsLineBarSalesById(Long id) {
        return AjaxResult.success(getById(id));
    }

    @Override
    public AjaxResult insertEchartsLineBarSales(EchartsLineBarSales echartsLineBarSales) {
        if (save(echartsLineBarSales)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult updateEchartsLineBarSales(EchartsLineBarSales echartsLineBarSales) {
        if (updateById(echartsLineBarSales)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult deleteEchartsLineBarSalesByIds(Long[] ids) {
        if (getBaseMapper().deleteEchartsLineBarSalesBatchByIds(ids) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}




